{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          C1        C2        C3        C4        C5        C6        C7  \\\n",
      "0   0.114966  0.184724  0.166652  0.364469  0.114966  0.920282  0.908159   \n",
      "1   0.190926  0.171085  0.146479  0.186095  0.190926  0.220848  0.370244   \n",
      "2   0.179959  0.130674  0.134502  0.102215  0.179959  0.088902  0.018531   \n",
      "3   0.065458  0.033119  0.037073  0.034201  0.065458  0.027551  0.016762   \n",
      "4   0.066054  0.021193  0.034832  0.020015  0.066054  0.013480  0.083422   \n",
      "5   0.111168  0.095789  0.133227  0.097760  0.111168  0.063498  0.092234   \n",
      "6   0.052484  0.044275  0.063632  0.033855  0.052484  0.029603  0.017276   \n",
      "7   0.071883  0.047982  0.039291  0.080140  0.071883  0.030183  0.007339   \n",
      "8   0.215152  0.251137  0.312073  0.232922  0.215152  1.000000  1.000000   \n",
      "9   1.000000  1.000000  0.934835  1.000000  1.000000  0.395716  0.105852   \n",
      "10  0.717555  0.963611  0.490858  0.717138  0.717555  0.977907  0.173944   \n",
      "11  0.219253  0.297423  0.207645  0.297644  0.219253  0.076019  0.057480   \n",
      "12  0.224667  0.169983  0.162283  0.193532  0.224667  0.315292  0.097173   \n",
      "13  0.070883  0.080769  0.078770  0.085572  0.070883  0.056456  0.064739   \n",
      "14  0.546960  0.494684  0.612781  0.450697  0.546960  0.089778  0.097658   \n",
      "15  0.296895  0.170735  0.167283  0.173035  0.296895  0.060575  0.054812   \n",
      "16  0.196641  0.155942  0.198903  0.172725  0.196641  0.107523  0.059390   \n",
      "17  0.189860  0.111667  0.180919  0.126609  0.189860  0.049911  0.044039   \n",
      "18  0.931458  0.759600  1.000000  0.830857  0.931458  0.647468  0.224156   \n",
      "19  0.042961  0.048349  0.049213  0.101365  0.042961  0.020104  0.018079   \n",
      "20  0.007438  0.008463  0.005615  0.006584  0.007438  0.029132  0.101899   \n",
      "21  0.102175  0.128279  0.130305  0.192702  0.102175  0.080392  0.149185   \n",
      "22  0.128717  0.121616  0.117040  0.258013  0.128717  0.068069  0.024521   \n",
      "23  0.033706  0.028100  0.023009  0.042021  0.033706  0.011155  0.036621   \n",
      "24  0.037026  0.043488  0.034356  0.040404  0.037026  0.016189  0.095417   \n",
      "25  0.000000  0.000000  0.000000  0.000000  0.000000  0.008159  0.000000   \n",
      "26  0.102001  0.077254  0.089304  0.174276  0.102001  0.059686  0.028087   \n",
      "27  0.028407  0.022295  0.021302  0.033351  0.028407  0.002079  0.005080   \n",
      "28  0.002815  0.001836  0.005235  0.005329  0.002815  0.000000  0.190429   \n",
      "29  0.012299  0.016874  0.009796  0.009544  0.012299  0.030565  0.013432   \n",
      "30  0.016192  0.015877  0.017936  0.027898  0.016192  0.025473  0.039054   \n",
      "\n",
      "          C8        C9       C10  ...       C16       C17       C18       C19  \\\n",
      "0   1.000000  0.347591  0.806316  ...  0.262961  0.311798  0.160382  0.485756   \n",
      "1   0.644003  1.000000  0.723018  ...  0.108673  0.110974  0.175329  0.229865   \n",
      "2   0.213799  0.345456  0.023911  ...  0.059902  0.049531  0.642544  0.091811   \n",
      "3   0.186949  0.226636  0.074079  ...  0.030510  0.013477  0.233202  0.050131   \n",
      "4   0.391927  0.666887  0.030598  ...  0.024750  0.011568  0.350127  0.042485   \n",
      "5   0.518601  0.548178  0.094120  ...  0.157948  0.093026  0.354512  0.262309   \n",
      "6   0.387653  0.466293  0.021935  ...  0.038000  0.017584  0.247544  0.042598   \n",
      "7   0.280278  0.197251  0.020848  ...  0.035404  0.019674  0.259480  0.026027   \n",
      "8   0.851756  0.639753  1.000000  ...  0.672964  0.438856  0.188553  0.845056   \n",
      "9   0.603750  0.766440  0.162214  ...  0.480676  0.533140  0.768135  1.000000   \n",
      "10  0.689243  0.673016  0.244006  ...  0.293444  0.338563  0.725803  0.371484   \n",
      "11  0.135620  0.300676  0.031335  ...  0.043642  0.045939  0.385425  0.133933   \n",
      "12  0.471317  0.605567  0.165007  ...  0.231406  0.164384  0.696727  0.249586   \n",
      "13  0.095932  0.302349  0.007088  ...  0.061948  0.040609  0.198585  0.090470   \n",
      "14  0.492869  0.562011  0.083046  ...  0.243528  0.234630  0.893769  0.278581   \n",
      "15  0.190519  0.338154  0.026155  ...  0.072962  0.071327  0.592997  0.085512   \n",
      "16  0.381689  0.408350  0.077284  ...  0.075936  0.043696  0.714122  0.111816   \n",
      "17  0.219813  0.325260  0.020916  ...  0.050871  0.027788  0.517074  0.064282   \n",
      "18  0.515278  0.598738  0.185152  ...  1.000000  1.000000  0.982272  0.823312   \n",
      "19  0.102657  0.258338  0.019341  ...  0.035999  0.049117  0.449809  0.051954   \n",
      "20  0.136893  0.073921  0.077900  ...  0.026048  0.012777  0.056645  0.037399   \n",
      "21  0.312937  0.378454  0.096931  ...  0.043155  0.071999  0.415715  0.098503   \n",
      "22  0.198658  0.262876  0.029411  ...  0.093494  0.049213  1.000000  0.110762   \n",
      "23  0.000000  0.168374  0.024167  ...  0.012988  0.011069  0.469621  0.020701   \n",
      "24  0.038107  0.138633  0.032468  ...  0.033169  0.023079  0.549703  0.039381   \n",
      "25  0.086297  0.000000  0.000000  ...  0.000000  0.000000  0.030138  0.000000   \n",
      "26  0.209798  0.400329  0.054460  ...  0.052241  0.028960  0.416780  0.063544   \n",
      "27  0.049099  0.108955  0.034470  ...  0.017206  0.006889  0.195747  0.007255   \n",
      "28  0.063701  0.303243  0.038750  ...  0.001649  0.000999  0.055959  0.006920   \n",
      "29  0.065771  0.291106  0.032815  ...  0.003272  0.002766  0.000000  0.008941   \n",
      "30  0.045451  0.220038  0.077850  ...  0.010482  0.018359  0.267392  0.008359   \n",
      "\n",
      "         C20       C21       C22       C23       C24       C25  \n",
      "0   0.485756  0.019544  0.086368  0.000000  0.040371  1.000000  \n",
      "1   0.229865  0.022148  0.104274  1.000000  0.151936  0.576019  \n",
      "2   0.091811  0.082043  0.161028  0.000000  0.700968  0.377635  \n",
      "3   0.050131  0.139640  0.258536  0.448705  0.619789  0.302923  \n",
      "4   0.042485  0.272357  1.000000  0.059774  0.102118  0.400673  \n",
      "5   0.262309  0.093083  0.243270  0.606919  0.185706  0.311605  \n",
      "6   0.042598  0.126433  0.355236  0.108136  0.446174  0.224219  \n",
      "7   0.026027  0.158059  0.312049  0.042769  0.332394  0.089861  \n",
      "8   0.845056  0.000000  0.000000  0.063490  0.077753  0.269585  \n",
      "9   1.000000  0.061117  0.032209  0.018706  0.257263  0.490191  \n",
      "10  0.371484  0.067025  0.058452  0.032444  0.058923  0.723502  \n",
      "11  0.133933  0.105618  0.105107  0.062291  0.646304  0.034798  \n",
      "12  0.249586  0.092140  0.138672  0.067660  0.241913  0.208630  \n",
      "13  0.090470  0.122019  0.148293  0.046319  0.871131  0.000000  \n",
      "14  0.278581  0.090071  0.077710  0.504115  0.387006  0.446505  \n",
      "15  0.085512  0.088714  0.079178  0.386570  1.000000  0.151715  \n",
      "16  0.111816  0.159790  0.108496  0.039686  0.556982  0.071159  \n",
      "17  0.064282  0.124634  0.103094  0.033747  0.577996  0.023606  \n",
      "18  0.823312  0.061126  0.038795  0.051066  0.378501  0.345683  \n",
      "19  0.051954  0.080970  0.189109  0.048062  0.246452  0.014970  \n",
      "20  0.037399  0.101647  0.203736  0.151865  0.067393  0.096887  \n",
      "21  0.098503  0.174004  0.096208  0.039833  0.184823  0.111364  \n",
      "22  0.110762  0.139625  0.075458  0.043376  0.487371  0.127778  \n",
      "23  0.020701  0.200322  0.130545  0.055097  0.548438  0.044633  \n",
      "24  0.039381  0.187451  0.091961  0.068858  0.219323  0.141081  \n",
      "25  0.000000  1.000000  0.483289  0.000000  0.000000  0.022965  \n",
      "26  0.063544  0.166577  0.217740  0.189377  0.230621  0.239292  \n",
      "27  0.007255  0.204812  0.278426  0.162716  0.143407  0.023054  \n",
      "28  0.006920  0.520372  0.821886  0.053470  0.017112  0.276912  \n",
      "29  0.008941  0.187441  0.375905  0.294154  0.878658  0.313101  \n",
      "30  0.008359  0.296482  0.496126  0.000000  0.061537  0.223750  \n",
      "\n",
      "[31 rows x 25 columns]\n",
      "                                                    \n",
      "         权重如下\n",
      "C1   0.047928\n",
      "C2   0.053287\n",
      "C3   0.050227\n",
      "C4   0.044571\n",
      "C5   0.047928\n",
      "C6   0.068977\n",
      "C7   0.061928\n",
      "C8   0.024536\n",
      "C9   0.012793\n",
      "C10  0.066527\n",
      "C11  0.023335\n",
      "C12  0.018924\n",
      "C13  0.023323\n",
      "C14  0.017772\n",
      "C15  0.018046\n",
      "C16  0.059996\n",
      "C17  0.067787\n",
      "C18  0.017696\n",
      "C19  0.053915\n",
      "C20  0.053915\n",
      "C21  0.028540\n",
      "C22  0.028927\n",
      "C23  0.056137\n",
      "C24  0.024368\n",
      "C25  0.028618\n",
      "运行完成!\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "\"\"\"\n",
    "Created on Fri Mar 23 10:48:36 2018\n",
    "@author: Big Teacher Brother\n",
    "\"\"\"\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import math\n",
    "from numpy import array\n",
    "\n",
    "# 1读取数据\n",
    "df = pd.read_csv('C:/Users/Administrator/Desktop/整理的数据/2015a.csv', encoding='gb2312')\n",
    "# 2数据预处理 ,去除空值的记录\n",
    "df.dropna()\n",
    "\n",
    "\n",
    "# 定义熵值法函数\n",
    "def cal_weight(x):\n",
    "    '''熵值法计算变量的权重'''\n",
    "    # 标准化\n",
    "    x = x.apply(lambda x: ((x - np.min(x)) / (np.max(x) - np.min(x))))\n",
    "    print(x)\n",
    "    # 求k\n",
    "    rows = x.index.size  # 行\n",
    "    cols = x.columns.size  # 列\n",
    "    k = 1.0 / math.log(rows)\n",
    "\n",
    "    lnf = [[None] * cols for i in range(rows)]\n",
    "\n",
    "    # 矩阵计算--\n",
    "    # 信息熵\n",
    "    # p=array(p)\n",
    "    x = array(x)\n",
    "    lnf = [[None] * cols for i in range(rows)]\n",
    "    lnf = array(lnf)\n",
    "    for i in range(0, rows):\n",
    "        for j in range(0, cols):\n",
    "            if x[i][j] == 0:\n",
    "                lnfij = 0.0\n",
    "            else:\n",
    "                p = x[i][j] / x.sum(axis=0)[j]\n",
    "                lnfij = math.log(p) * p * (-k)\n",
    "            lnf[i][j] = lnfij\n",
    "    lnf = pd.DataFrame(lnf)\n",
    "    E = lnf\n",
    "\n",
    "    # 计算冗余度\n",
    "    d = 1 - E.sum(axis=0)\n",
    "    # 计算各指标的权重\n",
    "    w = [[None] * 1 for i in range(cols)]\n",
    "    for j in range(0, cols):\n",
    "        wj = d[j] / sum(d)\n",
    "        w[j] = wj\n",
    "        # 计算各样本的综合得分,用最原始的数据\n",
    "\n",
    "    w = pd.DataFrame(w)\n",
    "    return w\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    # 计算df各字段的权重\n",
    "    w = cal_weight(df)  # 调用cal_weight\n",
    "    w.index = df.columns\n",
    "    print('                                                    '\n",
    "          ''\n",
    "          '')\n",
    "    w.columns = ['权重如下']\n",
    "    print(w)\n",
    "    print('运行完成!')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "0.047928,0.053287,0.050227,0.044571,0.047928,0.068977,0.061928,0.024536,0.012793,0.066527,0.023335,0.018924,0.023323,0.017772,0.018046,0.059996,0.067787,0.017696,0.053915,0.053915,0.028540,0.028927,0.056137,0.024368,0.028618"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
